package com.bbt.dormassistant.controller;


import com.bbt.dormassistant.service.SecondHandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "二手市场接口")
@RestController
@RequestMapping("/secondHand")
public class SecondHandController {


    @Autowired
    private SecondHandService secondHandService;



    @ApiOperation(value = "添加二手市场交易记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题", required = true, dataType = "String"),
            @ApiImplicitParam(name = "content", value = "内容", required = true, dataType = "String")
    })
    @PostMapping(value = "/addSecondHand")
    public Map addSecondHand(@RequestParam(value = "userId") String userId,
                             @RequestParam(value = "title") String title,
                             @RequestParam(value = "content") String content
                             ){
        Map<String,Object> map = new HashMap<>();
        map.put("success",secondHandService.addSecondHand(userId,title,content));
        return map;
    }




    @ApiOperation(value = "根据ID查询二手交易记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "secondHandId", value = "记录ID", required = true, dataType = "int")
    })
    @GetMapping("/querySecondHandById")
    public Map querySecondHandById(@RequestParam(value = "secondHandId") Integer secondHandId){
        Map<String,Object> map = new HashMap<>();
        map.put("secondHand",secondHandService.queryBySecondHandId(secondHandId));
        return map;
    }



    @ApiOperation(value = "根据userID获取该用户的所有文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currIndex", value = "当前页码(可选项)", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页大小(可选项)",  dataType = "int")
    })
    @GetMapping("/queryAll")
    public Map queryAll(@RequestParam(value = "currIndex",required = false) Integer currIndex,
                        @RequestParam(value = "pageSize",required = false) Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("secondHands",secondHandService.queryAll(currIndex,pageSize));
        return map;
    }


}
